package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.model.Page;
import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.BusinessMoneyRecordDao;
import com.baichang.taoshunda.entity.BusinessMoneyRecord;

import java.util.Map;

@Repository
public class BusinessMoneyRecordDaoImpl extends BaseDaoImpl<BusinessMoneyRecord> implements BusinessMoneyRecordDao {

    /**
     * 保证金查询
     *
     * @param typeId
     * @param is
     * @return
     */
    @Override
    public Map<String, Object> queryTypeMoney(Integer typeId, Integer is) {
        if (is == 1) {
            //假一罚三
            String sql = "SELECT id,money FROM t_buss_type WHERE id=? AND state=1";
            return queryForMap(sql, typeId);
        } else {
            //保证金
            String sql = "SELECT id,price AS money FROM t_buss_type WHERE id=? AND state=1";
            return queryForMap(sql, typeId);
        }
    }

    /**
     * 审核退款保证金
     *
     * @param id
     * @param
     * @return
     */
    @Override
    public int updateStatus(Integer id, Integer status) {
        if (status == 3) {
            String sql = "UPDATE {table} SET status=?,return_money_time=NOW() WHERE id=?";
            return update(sql, status, id);
        } else {
            String sql = "UPDATE {table} SET status=? WHERE id=?";
            return update(sql, status, id);
        }
    }

    /**
     * @param businessName
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryAuditingDepsoit(String businessName, int nowPage, int pageSize) {
        String sql = "SELECT mr.*,tb.buss_name AS busName FROM t_business_money_record mr INNER JOIN t_bussiness tb ON tb.id=mr.buiness_ud WHERE mr.status IN (2,3) ";
        if (!businessName.equals("")) {
            sql += " AND tb.buss_name LIKE ?";
            return pageForMapList(sql, nowPage, pageSize, "%" + businessName + "%");
        }
        return pageForMapList(sql, nowPage, pageSize);
    }

    /**
     * 查询保证金的记录
     *
     * @param businessName
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryRecord(String businessName, int nowPage, int pageSize) {
        String sql = "SELECT mr.*,tb.buss_name AS busName FROM t_business_money_record mr INNER JOIN t_bussiness tb ON tb.id=mr.buiness_ud WHERE mr.status IN (1,2,3)";
        if (!businessName.equals("")) {
            sql += " AND tb.buss_name LIKE ?";
            return pageForMapList(sql, nowPage, pageSize, "%" + businessName + "%");
        }
        sql += " ORDER BY created DESC";
        return pageForMapList(sql, nowPage, pageSize);
    }

    /**
     * @param busId
     * @param is
     * @return
     */
    @Override
    public Map<String, Object> getRecordInfo(Integer busId, Integer is) {
        String sql = "SELECT COUNT(*) AS num,id,status FROM t_business_money_record WHERE buiness_ud=? AND status IN (1,2) AND type=?";
        return queryForMap(sql, busId, is);
    }

    @Override
    public int getCountAboutStates(Integer id) {
        String sql = "SELECT COUNT(*) AS num FROM t_business_money_record WHERE buiness_ud=? AND status IN (2) AND type=2";
        return queryForInt(sql, id);
    }
}